package com.jinmdz.fmis.dao.dao.provider;

import com.jinmdz.fmis.dao.base.BaseProvider;
import com.jinmdz.fmis.dao.model.recinfo.RecInfoDispatchSearch;
import com.jinmdz.fmis.dao.model.recinfo.RecInfoSearh;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 根据时间有appId查询接运信息
 *
 * @author GaoXiangLiang
 * @date 2020/6/29 9:54
 */

public class RecInfoProvider extends BaseProvider {

    /**
     * 根据微信appId及创建时间查询接运信息
     *
     * @param data 查询实体
     * @return
     * @author GaoXiangLiang
     * @date 2020/6/19  17:11
     */


    public String listRecInfo(@Param("data") RecInfoSearh data) {

        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append("  id,deleted,rec_code,contact,contact_phone,contact_type,dead_name,death_cause,");
                builder.append("  relation_code,relation,car_type_code,car_type,purpose_code,purpose,");
                builder.append("  purpose_code,purpose,arrival_time,rec_charge,body_deposit_code,body_deposit,");
                builder.append("  address,sourceCode,source,task_state_code,task_state,chargeRemark,remark,appId,");
                builder.append("  funeral_parlour_code,create_user_id,gmt_create,unmodified,version");
                SELECT(builder.toString());
                FROM(" rec_info ");
                WHERE("  deleted = 0 ");

                // appId
                if (valid(data.getAppId())) {
                    WHERE(" appId like concat('%',#{data.appId},'%') ");
                }
                // 接运二维码
                if (valid(data.getRecCode())) {
                    WHERE(" rec_code like concat('%',#{data.recCode},'%') ");
                }
                // 到馆时间
                if (data.getGmtCreate() != null) {
                    WHERE(" DATE(gmt_create) >= #{data.gmtCreate,jdbcType=DATE} ");
                }
                // 到馆时间
                if (data.getGmtCreate() != null) {
                    WHERE(" DATE(gmt_create) <= #{data.gmtCreate,jdbcType=DATE} ");
                }
                ORDER_BY("  gmt_create desc ");
            }
        }.toString();
    }

    /**
     * 获得指定日期的未完成接运信息列表
     *
     * @param data 查询实体
     * @return
     * @author GaoXiangLiang
     * @date 2020/6/19  17:11
     */


    public String loadRecInfoAppNotFinishList(@Param("data") RecInfoSearh data) {

        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append("  id,deleted,rec_code,contact,contact_phone,contact_type,dead_name,death_cause,");
                builder.append("  relation_code,relation,car_type_code,car_type,purpose_code,purpose,");
                builder.append("  purpose_code,purpose,arrival_time,rec_charge,body_deposit_code,body_deposit,");
                builder.append("  address,sourceCode,source,task_state_code,task_state,chargeRemark,remark,appId,");
                builder.append("  funeral_parlour_code,create_user_id,gmt_create,unmodified,version");
                SELECT(builder.toString());
                FROM(" rec_info ");
                WHERE(" (task_state_code !=3 and task_state_code !=4) and  deleted = 0 ");

                // appId
                if (valid(data.getAppId())) {
                    WHERE(" appId like concat('%',#{data.appId},'%') ");
                }
                // 接运二维码
                if (valid(data.getRecCode())) {
                    WHERE(" rec_code like concat('%',#{data.recCode},'%') ");
                }
                // 到馆时间
                if (data.getGmtCreate() != null) {
                    WHERE(" DATE(gmt_create) >= #{data.gmtCreate,jdbcType=DATE} ");
                }
                // 到馆时间
                if (data.getGmtCreate() != null) {
                    WHERE(" DATE(gmt_create) <= #{data.gmtCreate,jdbcType=DATE} ");
                }
                ORDER_BY("  gmt_create desc ");
            }
        }.toString();
    }

    /**
     * 查询RecInfo列表
     *
     * @param data 数据实体
     * @return String
     * @author generator
     * @date 2020-06-16 10:19
     */
    public String listRecInfoSearch(@Param("data") RecInfoDispatchSearch data) {
        SQL list = new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" id,deleted,rec_code,contact,contact_phone,contact_type,dead_name,death_cause,relation_code,relation,car_type_code,car_type,purpose_code,purpose,arrival_time,rec_charge,body_deposit_code,body_deposit,address,sourceCode,source,task_state_code,task_state,chargeRemark,remark,appId,funeral_parlour_code,create_user_id,gmt_create,unmodified,modified_user_id,gmt_modified,version ");
                SELECT(builder.toString());
                FROM(" rec_info  ");
                WHERE("  deleted = 0 ");
                if (valid(data.getCarTypeCode())) {
                    WHERE(" car_type_code = #{data.carTypeCode,jdbcType=TINYINT} ");
                }
                if (valid(data.getRecCode())) {
                    WHERE(" rec_code like concat('%',#{data.recCode},'%') ");
                }
                if (valid(data.getContact())) {
                    WHERE(" contact like concat('%',#{data.contact},'%') ");
                }
                if (valid(data.getContactPhone())) {
                    WHERE(" contact_phone like concat('%',#{data.contactPhone},'%') ");
                }
                if (valid(data.getTaskStateCode()) || (data.getTaskStateCode() != null && data.getTaskStateCode() == 0)) {
                    WHERE(" task_state_code = #{data.taskStateCode,jdbcType=TINYINT} ");
                }
                ORDER_BY("  arrival_time desc ");
            }
        };
        return list.toString();
    }

}
